/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-07-10
 * Time: 16:03
 */
public class MyQueue {
    static class Node {
        int val;
        MyDeque.Node prev;
        MyDeque.Node next;

        Node(int val) {
            this.val = val;
        }
    }
    private MyDeque.Node head; //队头
    private MyDeque.Node last; //队尾
    private int usedSize;

    //尾插
    public void offer(int e) {
        MyDeque.Node node = new MyDeque.Node(e);
        if (head == null) { // 第一次插入
            head = node;
        }else {
            //不是第一次插入
            last.next = node;
            last.next.prev = last;
        }
        last = node;
        usedSize++;
    }
    //头删
    public int poll() {
        if(isEmpty()) {
            throw new NullPointerException("链表无数据!");
        }
        int val = head.val;
        if (head == last) { //只有一个节点
            head = null;
            last = null;
        } else {
            head = head.next;
            head.prev.next = null;
            head.prev = null;
        }
        usedSize--;
        return val;
    }
    //获取队头元素,获取链表中第一个节点的值
    public int peek(){
        if (isEmpty()) {
            throw new NullPointerException("链表无元素!");
        }
        return head.val;
    }

    public int size() {
        return usedSize;
    }
    public boolean isEmpty() {
        if (head == null) {
            return true;
        }
        return false;
    }
}
